package cheqex.update.controllers;
import java.util.ArrayList;
import java.sql.Date;
import java.util.List;
import cheqex.dao.CollectionCashPeer;
import cheqex.dao.CollectionCheckPeer;
import cheqex.dao.CollectionCash;
import cheqex.dao.CollectionCashPeer;
import cheqex.dao.CollectionCheck;
import cheqex.dao.CollectionCheckPeer;


public class CollectionController {
	private String date;
	
	public CollectionController(String userDate){
		this.date = userDate;
	}

	public static boolean addCollectionCash(String orNumber, java.sql.Date date, int amount, String particular, int id){
		try{
			if(CollectionCashPeer.addCollectionCash(orNumber, amount, particular, date, id))
				return true;
			else
				return false;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean editCollectionCash(String orNumber, int amount, String particular, java.sql.Date date){
		try{
			CollectionCashPeer.editCollectionCash(orNumber, amount, particular, date);
			return true;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean deleteCollectionCash(String orNumber, java.sql.Date date){
		try{
			CollectionCashPeer.deleteCollectionCash(orNumber, date);
			return true;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean addCollectionCheck(String number, java.sql.Date date, int amount, String particular, int id){
		try{
			if(CollectionCheckPeer.addCollectionCheck(number, amount, particular, date, id))
				return true;
			else
				return false;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean editCollectionCheck(String number, int amount, String particular, java.sql.Date date){
		try{
			CollectionCheckPeer.editCollectionCheck(number, amount, particular, date);
			return true;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean deleteCollectionCheck(String number, java.sql.Date date){
		try{
			CollectionCheckPeer.deleteCollectionCheck(number, date);
			return true;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static int validateDataToBeAdded(String number, int amount, String particular){
		int error_id=8;
		if(number.isEmpty()){
			if(Integer.toString(amount).isEmpty() || amount==-9999){
				if(particular.isEmpty())error_id=3; //missing number & amount & particular
				else error_id=2; //missing number & amount 
			}else if(particular.isEmpty()) error_id=4; //missing number & particular
			else error_id=1;//missing number
		}else if(Integer.toString(amount).isEmpty() || amount==-9999){
			if(particular.isEmpty()) error_id = 5; //missing amount & particular
			else error_id=6; //missing amount
		}else if(particular.isEmpty()) error_id=7; //missing particular
		
		return error_id;
	}
	
	public static boolean isNumeric(String input){ //checks if number
		try 
		{
			 char[] ch = input.toCharArray(); 
			 for(int i = 0; i < input.length();i++) {
		            if( Character.isDigit(ch[i]) ) 
		            	continue; 
		            else return false;
			 }
			 return true;
		}catch (NumberFormatException nfe) {
			nfe.printStackTrace();
			return false;
		}
	
	}
	
	
	public static boolean findOR(String orNumber, java.sql.Date date){
		try{
			if(CollectionCashPeer.getOR(orNumber, date)!=null)
				return true;
			else return false;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean findPR(String number, java.sql.Date date){
		try{
			if(CollectionCheckPeer.getPR(number, date)!=null)
				return true;
			else return false;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static int getAmtCash(String key){
		try{
			List<CollectionCash> list = CollectionCashPeer.getData(key);
			CollectionCash collectionCash = list.get(0);
			return collectionCash.getAmount();
		}catch (Exception e){
			e.printStackTrace();
			return -9999;
		}
	}
	
	public static String getParticularCash(String key){
		try{
			List<CollectionCash> list = CollectionCashPeer.getData(key);
			CollectionCash collectionCash = list.get(0);
			return collectionCash.getParticulars();
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public static int getAmtCheck(String key){
		try{
			List<CollectionCheck> list = CollectionCheckPeer.getData(key);
			CollectionCheck collectionCheck = list.get(0);
			return collectionCheck.getAmount();
		}catch (Exception e){
			e.printStackTrace();
			return -9999;
		}
	}
	
	public static String getParticularCheck(String key){
		try{
			List<CollectionCheck> list = CollectionCheckPeer.getData(key);
			CollectionCheck collectionCheck = list.get(0);
			return collectionCheck.getParticulars();
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}

}
